package com.xmy.cultivate.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xmy.cultivate.entity.TeacherPoint;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface TeacherPointMapper extends BaseMapper<TeacherPoint> {
    @Select({ "<script>",
            "SELECT * FROM (",
                "SELECT",
                    " p_b_t.id as id," ,
                    " GROUP_CONCAT( p_b_t.id ) AS id_str,",
                    " p_b_t.teacher_id,",
                    " GROUP_CONCAT( p_b_t.course_type_id ) AS course_type_id_str,",
                    " p_b_t.year_part AS year_part,",
                    " p_b_t.quarter_num AS quarter_num,",
                    " p_b_t.lesson_type,",
                    " CASE WHEN p_b_t.lesson_type = 1 THEN '精品课'",
                    " WHEN p_b_t.lesson_type = 2 THEN '智能课'",
                    " WHEN p_b_t.lesson_type = 3 THEN '刷题班'",
                    " WHEN p_b_t.lesson_type = 4 THEN '教学艺术'",
                    " END AS lesson_type_name,",
                    " p_b_t.teacher_type,",
                    " s.`name` AS teacher_name,",
                    " p_b_t.year_class_id AS grade,",
                    " GROUP_CONCAT( c_t.`name` ) AS course_type,",
                    " s_j.NAME AS subject,",
                    " '手动添加' AS entity_type, ",
                    " p_b_t.type, ",
                    "getDictName ( p_b_t.type, 45 ) AS type_str, ",
                    "p_b_t.version_id, ",
                    "e_v.name AS version_name ",
                    " FROM",
                    " practice_brush_teacher p_b_t",
                    " LEFT JOIN staff s ON p_b_t.teacher_id = s.id",
                    " JOIN course_type c_t ON p_b_t.course_type_id = c_t.id",
                    " JOIN subjects s_j ON s_j.id = p_b_t.subjects_id ",
                    " LEFT JOIN edu_version e_v ON e_v.id = p_b_t.version_id ",
                    " WHERE",
                    " p_b_t.deleted = 0 ",
                    " AND s.deleted = 0 ",
                    " AND c_t.deleted = 0 ",
                    " AND s_j.deleted = 0 ",
                    "<if test ='subjectId != null and subjectId !=&apos;&apos;'>",
                        "AND p_b_t.subjects_id = ${subjectId} ",
                    "</if>",
                    "<if test ='yearPart != null and yearPart !=&apos;&apos;'>",
                        "AND p_b_t.year_part = ${yearPart} ",
                    "</if>",
                    "<if test ='quarterNum != null and quarterNum !=&apos;&apos;'>",
                        "AND p_b_t.quarter_num = ${quarterNum} ",
                    "</if>",
                    "<if test ='lessonType != null and lessonType !=&apos;&apos;'>",
                        "AND p_b_t.lesson_type = ${lessonType}",
                    "</if>",
                    "<if test ='teacherName != null and teacherName !=&apos;&apos;'>",
                        "AND s.name LIKE '%${teacherName}%' ",
                    "</if>",
                    "<if test ='yearClassId != null and yearClassId !=&apos;&apos;'>",
                        "AND p_b_t.year_class_id = ${yearClassId} ",
                    "</if>",
                    "<if test ='type != null and type !=&apos;&apos;'>",
                        "AND p_b_t.type IN ( ${type})",
                    "</if>",
                    "<if test ='teacherType != null and teacherType !=&apos;&apos;'>",
                        "AND p_b_t.teacher_type IN (${teacherType})",
                    "</if>",
                    "<if test ='versionId != null and versionId !=&apos;&apos;'>",
                        "AND p_b_t.version_id IN (${versionId})",
                    "</if>",
                    "GROUP BY p_b_t.teacher_id,p_b_t.lesson_type,p_b_t.teacher_type,p_b_t.year_class_id,p_b_t.type,p_b_t.version_id",
                ") AS t",
                "WHERE 1=1",
                "<if test ='courseTypeId != null and courseTypeId !=&apos;&apos;'>",
                    "AND ( find_in_set( ${courseTypeId}, course_type_id_str ))",
                "</if>",

                   /* " UNION",
                    " SELECT",
                    " t01.g_id as id,",
                    " t01.teacher_id,",
                    " t01.course_type_id,",
                    " t01.year_part as year_part,",
                    " t01.quarter_num as quarter_num,",
                    " t01.lesson_type as lesson_type,",
                    " CASE WHEN t01.lesson_type = 1 THEN '精品课'",
                    " WHEN t01.lesson_type = 2 THEN '智能课'",
                    " WHEN t01.lesson_type = 3 THEN '刷题班'",
                    "END AS lesson_type_name,",
                    " t01.t_name as teacher_name,",
                    " t01.year_class_id as grade,",
                    " t01.b_x_NAME as course_Type,",
                    " t01.k_m_NAME as subject,",
                    " '排班' AS entity_type",
                    " FROM",
                    "(",
                    "SELECT",
                    " g.id as g_id,",
                    " g.year_part AS year_part,",
                    " g.quarter_num AS quarter_num,",
                    " g.lesson_type AS lesson_type,",
                    " g.teacher_id AS teacher_id,",
                    " s.NAME AS t_name,",
                    " g.year_class_id AS year_class_id,",
                    " g.course_type_id AS course_type_id,",
                    " c_t.NAME AS b_x_NAME,",
                    " s_j.NAME AS k_m_NAME,",
                    " g.subjects_id as subjects_id,",
                    "( SELECT count( id ) FROM student_grade WHERE student_grade.grade_id = g.id ) AS student_count ",
                    " FROM",
                    " grade g",
                    " LEFT JOIN staff s ON s.id = g.teacher_id",
                    " JOIN course_type c_t ON c_t.id = g.course_type_id",
                    " JOIN subjects s_j ON s_j.id = g.subjects_id",
                    " JOIN student_grade s_g ON s_g.grade_id = g.id ",
                    " WHERE",
                    " g.deleted=0 ",
                    " AND s.deleted=0 ",
                    " AND c_t.deleted=0 ",
                    " AND s_j.deleted=0 ",
                    " AND s_g.deleted=0 ",
                    "<if test ='teacherName != null and teacherName !=&apos;&apos;'>",
                        "AND s.name LIKE '%${teacherName}%' ",
                    "</if>",
                    "<if test ='yearPart != null and yearPart !=&apos;&apos;'>",
                        "AND g.year_part = ${yearPart} ",
                    "</if>",
                    "<if test ='quarterNum != null and quarterNum !=&apos;&apos;'>",
                        "AND g.quarter_num = ${quarterNum} ",
                    "</if>",
                    "<if test ='lessonType != null and lessonType !=&apos;&apos;'>",
                        "AND g.lesson_type = ${lessonType} ",
                    "</if>",
                    "<if test ='yearClassId != null and yearClassId !=&apos;&apos;'>",
                        "AND g.year_class_id = ${yearClassId} ",
                    "</if>",
                    "<if test ='courseTypeId != null and courseTypeId !=&apos;&apos;'>",
                        "AND g.course_type_id = ${courseTypeId} ",
                    "</if>",
                    " GROUP BY",
                    " g.teacher_id,",
                    " g.year_class_id,",
                    " g.course_type_id ",
                    " ) AS t01 ",
                    " WHERE",
                    " t01.student_count > 0 ",
            "<if test ='subjectId != null and subjectId !=&apos;&apos;'>",
                "AND t01.subjects_id = ${subjectId} ",
            "</if>",
            "<if test ='yearPart != null and yearPart !=&apos;&apos;'>",
                "AND t01.year_part = ${yearPart} ",
            "</if>",
            "<if test ='quarterNum != null and quarterNum !=&apos;&apos;'>",
                "AND t01.quarter_num = ${quarterNum} ",
            "</if>",
            "<if test ='lessonType != null and lessonType !=&apos;&apos;'>",
                "AND t01.lesson_type = ${lessonType} ",
            "</if>",
            "<if test ='yearClassId != null and yearClassId !=&apos;&apos;'>",
                "AND t01.year_class_id = ${yearClassId} ",
            "</if>",
            "<if test ='courseTypeId != null and courseTypeId !=&apos;&apos;'>",
                "AND t01.course_type_id = ${courseTypeId} ",
            "</if>",*/
            "</script>"})
     public IPage<TeacherPoint> findAllTeachingPoint(
             IPage<TeacherPoint> page,
             @Param("yearPart") String yearPart,
             @Param("quarterNum") String quarterNum,
             @Param("lessonType") String lessonType,
             @Param("teacherName") String teacherName,
             @Param("yearClassId") String yearClassId,
             @Param("courseTypeId") String courseTypeId,
             @Param("subjectId") String subjectId,
             @Param("type") String type,
             @Param("teacherType") String teacherType,
             @Param("versionId") String versionId
    );
}
